// pages/batchAddRoomsConfig/batchAddRoomsConfig.js
var that = "";
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    price:'',
    area:'',
    configIndex: 0,
    configIndexLabel:'',
    roomTypeList: [],//户型
    roomtypeindex: 0,//选择户型下标
    payTypeList: [],//付款方式列表
    paytypeindex: 0,//付款方式下标
    roomsConf: [],//配置
    roomsConfigIndex:0,
  },

  //获取户型
  getRoomsType: function () {
    var that = this;
    wx.request({
      url: app.globalData.url + '/wx/dict/list',
      data: {
        key: 'HUXING',
      },
      success: function (res) {
        if (res.data.success) {
          that.setData({
            roomTypeList: res.data.data,
          })
        } else {
          wx.showModal({
            title: '提示',
            content: 'res.data.msg',
            showCancel: false,
          })
        }
      },
      fail: function () {
        wx.showModal({
          title: '提示',
          content: '网络异常',
          showCancel: false,
        })
      }
    })
  },
  //户型选择
  roomsTypeChabge: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value);
    this.setData({
      roomtypeindex: e.detail.value,
    })
  },
  //获取付款方式
  getPayType: function () {
    var that = this;
    wx.request({
      url: app.globalData.url + '/wx/dict/list',
      data: {
        key: 'CHARGEMODE',
      },
      success: function (res) {
        if (res.data.success) {
          that.setData({
            payTypeList: res.data.data,
          })
        } else {
          wx.showModal({
            title: '提示',
            content: 'res.data.msg',
            showCancel: false,
          })
        }

      },
      fail: function () {

      }
    })
  },
  //付款方式选择
  payTypeChabge: function (e) {
    this.setData({
      paytypeindex: e.detail.value,
    })
  },
  //面积输入框
  areaInput: function (e) {
    this.setData({
      area: e.detail.value
    })
  },
  //租金输入框
  priceInput: function (e) {
    this.setData({
      price: e.detail.value
    })
  },
  //点击添加房间配置
  addConfigClick: function () {
    if (this.data.roomsConf && this.data.roomsConf.length>0){
      wx.setStorageSync('conf', this.data.roomsConf)
    }
    wx.navigateTo({
      url: '../addConfig/addConfig',
    })
  },
  //点击删除房间配置
  deleteConfigClick: function (e) {
    var idx = e.target.dataset.index;
    console.log(idx)
    var conf = this.data.roomsConf;
    conf.splice(idx, 1)
    console.log(conf)
    this.setData({
      roomsConf: conf,
    })
  },
  saveConfig:function(){
    var config = {
      price: this.data.price,
      area: this.data.area,
      configIndex: this.data.configIndex,
      roomTypeName: this.data.roomTypeList[this.data.roomtypeindex].value,//户型名称
      roomtypeindex: this.data.roomtypeindex,//选择户型下标
      roomTypeKey: this.data.roomTypeList[this.data.roomtypeindex].key,//key
      payTypeName: this.data.payTypeList[this.data.paytypeindex].value,//付款名称
      payTypeKey: this.data.payTypeList[this.data.paytypeindex].key,//付款key
      paytypeindex: this.data.paytypeindex,//付款方式下标
      roomsConf: this.data.roomsConf,//配置
      roomsConfigIndex: this.data.roomsConfigIndex,//已关联的房间
    }
    console.log('config')
    console.log(config)
    wx.setStorageSync('config', config)
  },
  //确定
  submit:function(){
    this.saveConfig();
    wx.navigateBack();
  },
  //设置数据
  setConfig:function(data){
    this.setData({
      price: data.price,
      area: data.area,
      roomtypeindex: data.roomtypeindex,//选择户型下标
      paytypeindex: data.paytypeindex,//付款方式下标
      roomsConf: data.roomsConf,//配置
      roomsConfigIndex: data.roomsConfigIndex,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var index = options.add;
    console.log(index)
    if (index) {
      this.setData({
        configIndexLabel: index,
      })
      index--;
      this.setData({
        configIndex: index,
      })
    }
    //编辑数据获取
    index = options.edit;
    console.log('index')
    console.log(index)
    if (index) {
      var listConfig = wx.getStorageSync("listConfig")
      this.setConfig(listConfig[index]);
      this.setData({
        configIndex: index,
      })
      index++
      this.setData({
        configIndexLabel: index,
      })
     
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    that = this;
    this.getRoomsType();
    this.getPayType();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //添加房间配置返回
    var addConfig = wx.getStorageSync('addconfig');
    wx.removeStorageSync('addconfig')
    if (addConfig) {
      // addConfig = addConfig.concat(this.data.roomsConf)
      this.setData({
        roomsConf: addConfig,
      })
      console.log(this.data.roomsConf)
    }
    //应用到房号返回
    var configOnRooms = wx.getStorageSync('configOnRooms');
    wx.removeStorageSync('configOnRooms')
    if (configOnRooms) {
      this.setData({
        roomsConfigIndex: configOnRooms,
      })
      this.saveConfig();
    }
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})